<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Promise Compatible</title>
    <script type="text/javascript" src="promise.js"></script>
</head>
<body>

<script type="text/javascript">

    new Promise(function (resolve, reject) {
        console.log('Normal Promise, exec');   // 已修正, 仅调用一次
        setTimeout(function () {
            reject('Normal Promise');
        }, 500);
    }).then(function (data) {
        console.log(data + ', resolve');
    }).catch(function (err) {
        console.log(err + ', reject');
    }).finally(function () {
        console.log('Normal Promise, finally');
    });

    // Promise.all

    var p1 = new Promise(function(resolve, reject) {
        console.log('Promise all, exec');
        setTimeout(function () {
            reject('Promise all, reject1');
        }, 1500);
    });

    var p2 = new Promise(function(resolve, reject) {
       setTimeout(function () {
           resolve('Promise all, resolve');
       }, 500);
    });

    var p3 = new Promise(function(resolve, reject) {
        reject('Promise all, reject2');
    });

    Promise.all([p1, p2, p3]).then(function () {
        console.log(arguments[0]);
    }).catch(function () {
        console.log(arguments[0]);
    }).finally(function () {
        console.log('Promise all, finally, will wait.');
    });     // TODO: finally 需要解决 ....

    // Promise.race

    var p4 = new Promise(function(resolve, reject) {
        console.log('Promise race, exec');
        setTimeout(function () {
            reject('Promise race, reject1');
        }, 1000);
    });

    var p5= new Promise(function(resolve, reject) {
        setTimeout(function () {
            resolve('Promise race, resolve');
        }, 500);
    });

    var p6 = new Promise(function(resolve, reject) {
        reject('Promise race, reject2');
    });

    Promise.race([p4, p5, p6]).then(function () {
        console.log(arguments[0]);
    }).catch(function () {
        console.log(arguments[0]);
    }).finally(function () {
        console.log('Promise race, finally');
    });

</script>

</body>
</html>